package com.boat.starter.ruoyi.util;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.boat.starter.ruoyi.config.props.AdmAuthConsts;

import lombok.extern.slf4j.Slf4j;

/**
 * servlet工具类
 * 
 * @author 周江
 *
 */
@Slf4j
public class ServletUtils {

	/**
	 * 获取String参数
	 */
	public static String getParameter(String name) {
		return getRequest().getParameter(name);
	}

	/**
	 * 获得头参数
	 * 
	 * @param name
	 * @return
	 */
	public static String getHeader(String name) {
		return getRequest().getHeader(name);
	}

	/**
	 * 获取请求token
	 *
	 * @return token
	 */
	public static String getToken() {
		String token = getHeader(AdmAuthConsts.Token.HEADER);
		if (StringUtils.hasLength(token) && token.startsWith(AdmAuthConsts.Token.PREFIX)) {
			return token.replace(AdmAuthConsts.Token.PREFIX, "");
		}else {
			log.warn("无效的rms token格式");
			return null;
		}
	}

	public static HttpServletRequest getRequest() {
		return getRequestAttributes().getRequest();
	}

	private static ServletRequestAttributes getRequestAttributes() {
		RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
		return (ServletRequestAttributes) attributes;
	}

}
